﻿using System.Data.Services.Client;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace CPlase
{
    public class Azure
    {

        #region Basic

        #region BasicInstance

        public CloudStorageAccount Account;
        public TableServiceContext Context;

        public Azure()
        {
            this.Account = GetStorageAccount();
            this.Context = GetContext(Account);
        }

        public Azure(CloudStorageAccount Account, TableServiceContext Context)
        {
            this.Account = Account;
            this.Context = Context;
        }

        public DataServiceQuery<T> GetListQuery<T>(string TableName)
        {
            return Context.CreateQuery<T>(TableName);
        }

        public void AddObject(string TableName, object Object)
        {
            Context.AddObject(TableName, Object);
        }

        public void UpdateObject(object Object)
        {
            Context.UpdateObject(Object);
        }

        public void Commit()
        {
            this.Context.SaveChanges();
        }

        public void Close()
        {
            Context = null;
            Account = null;
        }

        #endregion

        #region BasicStatic

        private static CloudStorageAccount StorageAccount;

        public static CloudStorageAccount GetStorageAccount()
        {
            if (StorageAccount == null)
                //StorageAccount = CloudStorageAccount.Parse()

                StorageAccount = RoleEnvironment.IsEmulated ?
                    CloudStorageAccount.FromConfigurationSetting("EmulatedConnectionString")
                    : CloudStorageAccount.FromConfigurationSetting("ConnectionString");
            return StorageAccount;
        }

        public static TableServiceContext GetContext(CloudStorageAccount account)
        {
            var Context = new TableServiceContext(account.TableEndpoint.ToString(), account.Credentials);
            //Context.MergeOption = MergeOption.OverwriteChanges;
            //Context.SaveChangesDefaultOptions = SaveChangesOptions.Batch;
            //Batch Mode only with simular PartitionKey
            return Context;
        }

        public static TableServiceContext GetContext()
        {
            var account = GetStorageAccount();
            return new TableServiceContext(account.TableEndpoint.ToString(), account.Credentials);
        }

        public static CloudBlobClient GetBlobClient()
        {
            var account = GetStorageAccount();
            return account.CreateCloudBlobClient();
        }

        #endregion

        #endregion

        #region SimpleOperations

        public static DataServiceQuery<T> SimpleGetListQuery<T>(string TableName)
        {
            return GetContext().CreateQuery<T>(TableName);
        }

        public static void SimpleAddObject(string tableName, object Object)
        {
            var Context = GetContext();
            Context.AddObject(tableName, Object);
            Context.SaveChanges();
        }

        public static void SimpleDeleteObject(object Object)
        {
            var Context = GetContext();
            Context.DeleteObject(Object);
            Context.SaveChanges();
        }

        #endregion
    }
}